package com.bee.bootstrap.formDesign.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.bee.bootstrap.formDesign.entity.FormTemplate;
import com.bee.bootstrap.formDesign.mapper.FormTemplateMapper;
import com.bee.bootstrap.formDesign.service.IFormTemplateService;
import com.bee.bootstrap.user.entity.User;
import com.bee.bootstrap.user.service.IUserService;
import com.bee.common.core.enums.StatusEnum;
import com.bee.common.core.result.Result;
import com.bee.common.tools.context.SpringContextUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liekkas
 * @since 2018-08-26
 */
@Service
public class FormTemplateServiceImpl extends ServiceImpl<FormTemplateMapper, FormTemplate> implements IFormTemplateService {

    @Autowired
    private IUserService userServiceImpl;
    @Override
    public Result addTemplateForm(FormTemplate formDesign) {
        formDesign.setCreateTime(new Date());
        formDesign.setStatus(StatusEnum.NORMAL.toString());
        formDesign.setCreateUserId(Long.valueOf(SpringContextUtil.getRequest().getAttribute("userId").toString()));
        this.insert(formDesign);
        return Result.success(formDesign.getFormName()+"保存成功");
    }

    @Override
    public Result findPageView(Page<FormTemplate> page, FormTemplate formDesign) {
        formDesign.setCreateUserId(Long.valueOf(SpringContextUtil.getRequest().getAttribute("userId").toString()));
        formDesign.setStatus(StatusEnum.NORMAL.toString());
        EntityWrapper<FormTemplate> entityWrapper=new EntityWrapper<FormTemplate>(formDesign);
        //查询条件
        if(StringUtils.isNotBlank(formDesign.getFormNameQuery())){
            entityWrapper.like("form_name",formDesign.getFormNameQuery());
        }
        if(StringUtils.isNotBlank(formDesign.getFormCreateUserQuery())){
            List<User> users=userServiceImpl.findUsersLikeName(formDesign.getFormCreateUserQuery());
            if(users!=null&&users.size()>0){
                Set<Long> userIds=new HashSet<>();
                users.forEach(user -> {
                    userIds.add(user.getId());
                });
                entityWrapper.in("create_user_id",userIds);
            }
        }
        page.setOrderByField("create_time");
        page=this.selectPage(page,entityWrapper);
        List<FormTemplate> formDesignList=page.getRecords();
        Set<Long> userIds=new HashSet<Long>();
        if(formDesignList!=null&&formDesignList.size()>0){
            formDesignList.forEach(form->{
                userIds.add(form.getCreateUserId());
                form.setStatusView(StatusEnum.valueOf(form.getStatus()).getName());
            });
            List<User> users=userServiceImpl.findUsersByIds(userIds);
            formDesignList.forEach(form->{
                users.forEach(user->{
                    if(user.getId().compareTo(form.getCreateUserId())==0){
                        form.setCreateUserView(user.getName());
                    }
                });
            });
        }
        return Result.success(page);

    }

    @Override
    public Result deleteFormTemplateById(long id) {
        FormTemplate persistence=this.selectById(id);
        persistence.setStatus(StatusEnum.INVALID.toString());
        persistence.setUpdateTime(new Date());
        this.updateById(persistence);
        return Result.success("删除成功");
    }

    @Override
    public Result findFormTemplate(long id) {
        return Result.success(this.selectById(id));
    }

    @Override
    public Result updateTemplateForm(FormTemplate formDesign, Long formTemplateId) {
        formDesign.setId(formTemplateId);
        formDesign.setStatus(StatusEnum.NORMAL.toString());
        formDesign.setUpdateTime(new Date());
        this.updateById(formDesign);
        return Result.success("更新表单模板成功");
    }
}
